﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using TimberSmart.Data.Contracts;
using TimberSmart.Data.Repositories;

namespace TimberSmart.Data.Repositories
{
    public class RepositoryProvider
    {
        // Cache of all our factories.
        private RepositoryFactoryCollection _factories;

        public RepositoryProvider(RepositoryFactoryCollection factories)
        {
            _factories = factories;
        }

        // Get a preconfigured factory Func from RepositoryFactories.
        public Func<DbContext, object> GetRepositoryFactory<T>() where T: class
        {
            if (_factories.ContainsKey(typeof(T)))
            {
                return (Func<DbContext, object>)_factories[typeof(T)];
            }

            throw new InvalidOperationException(
                string.Format("No such repository {0} exists in EFRepositoryProvider", typeof(T).ToString()));
        }

    }
}
